package test.core.fun;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import hyl.core.struct.MyNode;
/**
 * 未完成???
 * @author 37798955@qq.com
 *
 * @param <T>
 */
public class HafumanTree<T> {

	private MyNode<Integer, T> build(List<MyNode<Integer, T>> nodes) {
		nodes = new ArrayList<MyNode<Integer, T>>(nodes);
		// sortList(nodes);
		while (nodes.size() > 1) {
			// createAndReplace(nodes);
		}
		return nodes.get(0);
	}

	/**
	 * 组合两个权值最小结点，并在结点列表中用它们的父结点替换它们
	 * 
	 * @param nodes 结点集合
	 */
	private void createAndReplace(List<MyNode<Integer, T>> nodes) {
		MyNode<Integer, T> left = nodes.get(0);
		MyNode<Integer, T> right = nodes.get(1);

		MyNode<Integer, T> parent = new MyNode<Integer, T>(left.get_id() + right.get_id());
		parent.setLeftChild(left);
		parent.setRightChild(right);
		nodes.remove(0);
		nodes.remove(0);
		nodes.add(parent);
		sortList(nodes);
	}

	/**
	 * 将结点集合由大到小排序
	 */

	private void sortList(List<MyNode<Integer, T>> nodes) {
		// Collections.sort(nodes);
	}

	/**
	 * 打印树结构，显示的格式是node(left,right)
	 * 
	 * @param node
	 */

	public void PrintTree(MyNode<Integer, T> node) {
		MyNode<Integer, T> left = null;
		MyNode<Integer, T> right = null;
		if (node != null) {
			System.out.print(node.getValue());
			left = node.getLeftChild();
			right = node.getRightChild();
			System.out.println("(" + (left != null ? left.getValue() : " ") + ","
					+ (right != null ? right.getValue() : " ") + ")");
		}
		if (left != null)
			PrintTree(left);
		if (right != null)
			PrintTree(right);
	}

}
